package com.song.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.song.commonutils.ResultObj;
import com.song.eduservice.client.vodfeign.VodFeign;
import com.song.eduservice.entity.*;
import com.song.eduservice.entity.frontvo.CourseFrontVo;
import com.song.eduservice.entity.frontvo.CourseWebVo;
import com.song.eduservice.entity.vo.CourseInfoVo;
import com.song.eduservice.entity.vo.CoursePublishVo;
import com.song.eduservice.mapper.EduCourseMapper;
import com.song.eduservice.service.EduChapterService;
import com.song.eduservice.service.EduCourseDescriptionService;
import com.song.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.song.eduservice.service.EduVideoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author song
 * @since 2020-03-29
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    private EduVideoService EduVideoService;

    @Autowired
    VodFeign vodFeign;

    @Autowired
    private EduChapterService eduChapterService;

    @Override
    public String saveCourse(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        //添加课程信息
        int result = baseMapper.insert(eduCourse);
        //添加课程简介
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(eduCourse.getId());
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescriptionService.save(eduCourseDescription);
        return eduCourse.getId();
    }

    @Override
    public CourseInfoVo findCourseByCourseId(String courseId) {
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse, courseInfoVo);
        EduCourseDescription byId = eduCourseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(byId.getDescription());
        return courseInfoVo;
    }

    @Override
    public int updateCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int result = baseMapper.updateById(eduCourse);
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescription.setId(courseInfoVo.getId());
        eduCourseDescriptionService.updateById(eduCourseDescription);
        return result;
    }

    @Override
    public CoursePublishVo getPublishCourseInfo(String courseId) {

        return baseMapper.getPublishCourseInfo(courseId);
    }

    @Override
    public ResultObj findAllCourse(long current, long limit, EduCourse eduCourse) {
        Page<EduCourse> page = new Page<>(current, limit);
        QueryWrapper<EduCourse> qw = new QueryWrapper<>();
        qw.eq(eduCourse.getStatus() != null, "status", eduCourse.getStatus());
        qw.like(StringUtils.isNotBlank(eduCourse.getTitle()), "title", eduCourse.getTitle());
        qw.orderByDesc("gmt_create");
        baseMapper.selectPage(page, qw);
        return ResultObj.ok().data("total", page.getTotal()).data("data", page.getRecords());
    }

    @Override
    public int deleteCourse(String courseId) {
        //查询课程中的视频（多个）批量删除视频
        QueryWrapper<EduVideo> deleteBatchVideo = new QueryWrapper();
        deleteBatchVideo.eq("course_id", courseId);
        deleteBatchVideo.select("video_source_id");
        List<EduVideo> videoList = EduVideoService.list(deleteBatchVideo);
        List<String> list = new ArrayList<>();
        for (EduVideo eduVideo : videoList) {
            if (null != eduVideo) {
                list.add(eduVideo.getVideoSourceId());
            }
        }
        if (list.size()>0) {
            vodFeign.deleteBatchVideo(list);
        }

        //1.根据课程id删除小节
        QueryWrapper<EduVideo> qwEduVideo = new QueryWrapper();
        qwEduVideo.eq("course_id", courseId);
        EduVideoService.remove(qwEduVideo);
        //2.根据课程id删除章节
        QueryWrapper<EduChapter> qwEduChapter = new QueryWrapper();
        qwEduChapter.eq("course_id", courseId);
        eduChapterService.remove(qwEduChapter);
        //3.根据课程id删除描述
        eduCourseDescriptionService.removeById(courseId);
        //4.根据课程id删除课程
        int result = baseMapper.deleteById(courseId);


        return result;
    }
    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageCourse, CourseFrontVo courseFrontVo) {
        //2 根据讲师id查询所讲课程
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("status","Normal");
        //判断条件值是否为空，不为空拼接
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())) { //一级分类
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())) { //二级分类
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())) { //关注度
            wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) { //最新
            wrapper.orderByDesc("gmt_create");
        }

        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {//价格
            wrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageCourse,wrapper);

        List<EduCourse> records = pageCourse.getRecords();
        long current = pageCourse.getCurrent();
        long pages = pageCourse.getPages();
        long size = pageCourse.getSize();
        long total = pageCourse.getTotal();
        boolean hasNext = pageCourse.hasNext();//下一页
        boolean hasPrevious = pageCourse.hasPrevious();//上一页

        //把分页数据获取出来，放到map集合
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        //map返回
        return map;
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }

}
